﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenerateAdminPage.Classes.Views
{
    #region USING
    using GenerateAdminPage.Classes.Base;
    using GenerateAdminPage.Classes.Helpers;
    using GenerateAdminPage.Classes.DBStructure;
    #endregion

    public abstract class AbstractViews : AbstractBase, IView
    {
        public const int OFFSET_Y = 26;
        public const int OFFSET_X = 323;
        private static GenerateType _type;
        /// <summary>
        /// Method is responsible for select generate type such as: view, frmclass or repository
        /// </summary>
        /// <returns>Correspding generate class</returns>
        public static AbstractBase SelectGenerateType(Table tbl, Table tblMany, GenerateType type)
        {
            _type = type;

            switch (type)
            {
                case GenerateType.SINGLE:
                    if (tbl.Attributes.Count > 3)
                    {
                        return new ViewSingleMoreAttrs();
                    }
                    else
                    {
                        return new ViewSingleLessAttrs();
                    }

                case GenerateType.COMPLEX:
                    return new ViewComplexes();

                case GenerateType.ONE_TO_MANY:
                    return new ViewOneToMany();

                case GenerateType.SELECT_MANY_FOR_ONE:
                    return new ViewSelectManyForOne();
            }
            return null;
        }

        public override string GenerateNameSpace(string nameSpace)
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine("namespace " + GlobalVariables.g_sNameSpace + "." + nameSpace);
            Result.AppendLine("{");
            Result.Append(GenerateUsingRegion());
            Result.Append(GenerateClass());
            Result.Append("}");

            return Result.ToString();
        }

        public override string GenerateUsingRegion()
        {
            return string.Empty;
        }

        public override string GenerateClass()
        {
            StringBuilder Result = new StringBuilder();

            switch (_type)
            {
                case GenerateType.SINGLE:
                case GenerateType.SELECT_MANY_FOR_ONE:
                    Result.AppendLine(TAB + "partial class FrmQuanLy" + TblOne.Name);
                    break;
                case GenerateType.COMPLEX:
                    Result.AppendLine(TAB + "partial class FrmLap" + TblOne.Name);
                    break;
                case GenerateType.ONE_TO_MANY:
                    Result.AppendLine(TAB + "partial class FrmCapNhat" + TblMany.Name);
                    break;
            }

            Result.AppendLine(TAB + "{");
            Result.AppendLine(TAB2 + "private System.ComponentModel.IContainer components = null;");
            Result.AppendLine(GenerateDispose());
            Result.AppendLine(GenerateInitializeComponent());
            Result.AppendLine(GenerateFields());
            Result.AppendLine(TAB + "}");

            return Result.ToString();
        }

        public string GenerateDispose()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "protected override void Dispose(bool disposing)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "if (disposing && (components != null))");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "components.Dispose();");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "base.Dispose(disposing);");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public virtual string GenerateFields()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.PanelEx panelEx1;");
            Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.PanelEx panelEx2;");
            Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.Controls.GroupPanel grpManageInfo;");
            Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.Controls.GroupPanel grpListData;");
            Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.LabelX lblManageName;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.Panel panel2;");
            Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.Controls.DataGridViewX dtgvData;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.Panel panel1;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.BindingNavigator binFunctions;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.ToolStripButton btnThem;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.ToolStripButton btnXoa;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.ToolStripButton btnCapNhat;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.ToolStripSeparator toolStripSeparator5;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.ToolStripButton btnThoat;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.ToolStripSeparator toolStripSeparator6;");
            Result.AppendLine(TAB2 + "private System.Windows.Forms.ToolStripButton btnReset;");

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.LabelX labelX" + (i + 1) + ";");
            }

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (TblOne.Attributes[i].IsForeignKey)
                {
                    Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.Controls.TextBoxX txt" + TblOne.Attributes[i].ReferTo + ";");
                    Result.AppendLine(TAB2 + "private System.Windows.Forms.Button btnChon" + TblOne.Attributes[i].ReferTo + ";");
                }
                else
                {
                    if (TblOne.Attributes[i].Type == DataType.DATETIME)
                    {
                        Result.AppendLine(TAB2 + "private DevComponents.Editors.DateTimeAdv.DateTimeInput dt" + TblOne.Attributes[i].Name + ";");
                    }
                    else if (TblOne.Attributes[i].Type == DataType.BOOL)
                    {
                        Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.Controls.ComboBoxEx cbx" + TblOne.Attributes[i].Name + ";");
                    }
                    else if (TblOne.Attributes[i].Type == DataType.IMAGE)
                    {
                        Result.AppendLine(TAB2 + "private System.Windows.Forms.PictureBox ptb" + TblOne.Attributes[i].Name + ";");
                        Result.AppendLine(TAB2 + "private System.Windows.Forms.Button btnChon" + TblOne.Attributes[i].Name + ";");
                        Result.AppendLine(TAB2 + "private System.Windows.Forms.Button btnXoa" + TblOne.Attributes[i].Name + ";");
                        Result.AppendLine(TAB2 + "private System.Windows.Forms.OpenFileDialog openFileDialog1;");
                    }
                    else
                    {
                        Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.Controls.TextBoxX txt" + TblOne.Attributes[i].Name + ";");
                    }
                }
            }

            if (Utils.HaveFKReferTo(DB, TblOne))
            {
                Result.AppendLine(TAB2 + "private System.Windows.Forms.ToolStripButton btnChon;");
            }

            return Result.ToString();
        }

        public abstract string GenerateInitializeComponent();
    }
}
